package com.ddone.util.mock;

import java.util.*;

/**
 * @author ddone
 * @date 2024/5/15-10:02
 */

public class RandomCollection {
    public static <T> T getOneFrom(List<T> list) {
        int randInt = RandomNum.getRandInt(0, list.size() - 1);
        return list.get(randInt);
    }

    public static <T> List<T> getSomeFrom(List<T> list, Integer num) {
        HashSet<Integer> idxSet = new HashSet();
        List<T> rslist = new ArrayList<>();
        while (idxSet.size() != num.intValue()) {
            int randIdx = RandomNum.getRandInt(0, list.size() - 1);
            if (!idxSet.contains(Integer.valueOf(randIdx))) {
                idxSet.add(Integer.valueOf(randIdx));
                rslist.add(list.get(randIdx));
            }
        }
        return rslist;
    }

    public static <K, V> V getOneFrom(Map<K, V> hashMap) {
        Object[] values = hashMap.values().toArray();
        if (values.length >= 1) {
            int randInt = RandomNum.getRandInt(0, values.length - 1);
            return (V)values[randInt];
        }
        return null;
    }

    public static <K, V> V getOneFrom(Map<K, V> hashMap, List<K> excludeKeyList) {
        HashMap<K, V> cloneMap = new HashMap<>();
        cloneMap.putAll(hashMap);
        for (K key : excludeKeyList) {
            cloneMap.remove(key);
        }
        Object[] values = cloneMap.values().toArray();
        if (values.length >= 1) {
            int randInt = RandomNum.getRandInt(0, values.length - 1);
            return (V)values[randInt];
        }
        return null;
    }
}
